package com.dong.order.controller;

import com.dong.springcloud.entities.CommonResult;
import com.dong.springcloud.entities.Payment;
import lombok.AllArgsConstructor;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Value;
import org.springframework.web.bind.annotation.*;
import org.springframework.web.client.RestTemplate;

import javax.annotation.Resource;

/**
 * @author DJ
 * @description 消费者控制层
 * @date 2021/12/25 11:00 下午
 **/
@RestController
// @AllArgsConstructor
@Slf4j
public class OrderController {

    // public static final String PAYMENT_URL = "http://localhost:9001";
    /**
     * 负载均衡后不需要聚体的服务ip：port 服务名即可
     */
    public static final String PAYMENT_URL = "http://cloud-payment-service";

    @Resource
    private RestTemplate restTemplate;

    @Value("${server.port}")
    private String serverPort;

    @GetMapping("comsumer/payment/get/{id}")
    public CommonResult<Payment> getPaymentById(@PathVariable("id") Long id) {
        log.debug("消费者服务serverPort:{}", serverPort);
        return restTemplate.getForObject(PAYMENT_URL + "/payment/get/" + id, CommonResult.class);
    }

    @PostMapping("comsumer/payment/insert")
    public CommonResult<Payment> insert(Payment payment) {
        log.debug("消费者服务serverPort:{}", serverPort);
        return restTemplate.postForObject(PAYMENT_URL + "/payment/insert/", payment, CommonResult.class);
    }
}
